﻿using OSS.BIM.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using OSS.BIM.Business.Utility;

namespace OSS.BIM.Business
{
    /// <summary>
    /// Class CurUserGroupEventArgs.
    /// </summary>
    /// <seealso cref="System.EventArgs" />
    public class SetCurUserGroupEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets the old project.
        /// </summary>
        /// <value>The old project.</value>
        public Project OldUserProject { get; set; }
        /// <summary>
        /// Gets or sets the current project.
        /// </summary>
        /// <value>The current project.</value>
        public Project CurUserProject { get; set; }
        /// <summary>
        /// Gets or sets the old feeder.
        /// </summary>
        /// <value>The old feeder.</value>
        public ModelGrope OldUserGroup { get; set; }
        /// <summary>
        /// Gets or sets the current feeder.
        /// </summary>
        /// <value>The current feeder.</value>
        public ModelGrope CurUserGroup { get; set; }
        /// <summary>
        /// Sets the old.
        /// </summary>
        /// <param name="curUserGroup">The current feeder.</param>
        internal void SetOld(CurUserGroup curUserGroup)
        {
            OldUserProject = curUserGroup.Project;
            OldUserGroup = curUserGroup.ModelGrope;
        }
        /// <summary>
        /// Sets the current.
        /// </summary>
        /// <param name="curUserGroup">The current feeder.</param>
        internal void SetCur(CurUserGroup curUserGroup)
        {
            CurUserProject = curUserGroup.Project;
            CurUserGroup = curUserGroup.ModelGrope;
        }
    }

    /// <summary>
    /// Delegate CurUserGroupEventHandler
    /// </summary>
    /// <param name="args">The <see cref="CurUserGroupEventArgs"/> instance containing the event data.</param>
    public delegate void CurUserGroupEventHandler(SetCurUserGroupEventArgs args);
    public class CurUserGroup
    {
        /// <summary>
        /// 当前用户
        /// </summary>
        public static CurUserGroup Instance;

        public CurUserGroup()
        {
            m_userGroup = null;
            m_userProject = null;
        }

        /// <summary>
        /// m_userGroup
        /// </summary>
        private ModelGrope m_userGroup;

        /// <summary>
        /// The m_userProject
        /// </summary>
        private Project m_userProject;

        /// <summary>
        /// The set current project feeder event
        /// </summary>
        [XmlIgnore]
        public CurUserGroupEventHandler SetCurGroupEvent;

        public ModelGrope ModelGrope
        {
            get { return m_userGroup; }
            set
            {
                var args = new SetCurUserGroupEventArgs();
                args.SetOld(this);

                m_userGroup = value;
                if (m_userGroup != null)
                    m_userProject = m_userGroup.GetProject();

                args.SetCur(this);
                if (SetCurGroupEvent != null)
                    SetCurGroupEvent(args);
            }
        }

        public Project Project
        {
            get { return m_userProject; }
            set
            {
                var args = new SetCurUserGroupEventArgs();
                args.SetOld(this);

                m_userProject = value;
                if (m_userProject != null)
                {
                    if (m_userGroup != null && m_userGroup.ProjectId != m_userProject.ProjectId || m_userGroup == null)
                    {
                        m_userGroup = m_userProject.GetFirstUserGroup();
                    }
                }
                else
                {
                    m_userGroup = null;
                }

                args.SetCur(this);
                if (SetCurGroupEvent != null)
                    SetCurGroupEvent(args);
            }
        }
    }
}
